﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class UserGetPassword: SkinnedWebControl
    {
        private string sessionKey = "GetPasswordSessionKey";
        private System.Web.UI.WebControls.Panel setup1;
        private System.Web.UI.WebControls.Panel setup2;
        private System.Web.UI.WebControls.Panel setup3;
        private TextBox txtUserName;
        private TextBox txtAnswer;
        private Label lbQuestion;
        private Label lbSucceedMessage;
        private IButtonControl btnSetup1Next;
        private IButtonControl btnSetup2Back;
        private IButtonControl btnSetup2Next;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            setup1 = (System.Web.UI.WebControls.Panel)skin.FindControl("setup1");
            setup2 = (System.Web.UI.WebControls.Panel)skin.FindControl("setup2");
            setup3 = (System.Web.UI.WebControls.Panel)skin.FindControl("setup3");
            txtUserName = (TextBox)skin.FindControl("txtUserName");
            txtAnswer = (TextBox)skin.FindControl("txtAnswer");
            lbQuestion = (Label)skin.FindControl("lbQuestion");
            lbSucceedMessage = (Label)skin.FindControl("lbSucceedMessage");
            btnSetup1Next = (IButtonControl)skin.FindControl("btnSetup1Next");
            btnSetup2Back = (IButtonControl)skin.FindControl("btnSetup2Back");
            btnSetup2Next = (IButtonControl)skin.FindControl("btnSetup2Next");

            btnSetup1Next.Text = Page.Strings["Next"];
            btnSetup2Next.Text = Page.Strings["Next"];
            btnSetup2Back.Text = Page.Strings["Prev"];
            setup1.Visible = true;
            setup2.Visible = false;
            setup3.Visible = false;

            btnSetup1Next.Click += new EventHandler(btnSetup1Next_Click);
            btnSetup2Back.Click += new EventHandler(btnSetup2Back_Click);
            btnSetup2Next.Click += new EventHandler(btnSetup2Next_Click);
        }

        void btnSetup2Next_Click(object sender, EventArgs e)
        {
            UserInfo info = Page.Session[sessionKey] as UserInfo;
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError,Page.Urls.UserGetPassword());
                return;
            }
            else
            {
                string answer = txtAnswer.Text.Trim();
                if (answer == info.PasswordAnswer)
                {
                    Users.ChangePassword(info.Id, "123456");
                    Email.SendMailGetPassword(info.Email,info.LoginName,"123456");

                    setup2.Visible = false;
                    setup3.Visible = true;

                    SiteContext.RenderMessage(ExceptionType.UserGetPasswordSuccess, SiteUrls.Urls.UserLogin());

                    Page.Session.Remove(sessionKey);
                }
                else
                {
                    setup1.Visible = false;
                    setup2.Visible = true;
                    lbQuestion.Text = Enum<QuestionType>.GetEnumStr(info.PasswordQuestion);
                    SiteContext.RenderMessage(ExceptionType.UserPasswordAnswerError);
                }
            }
        }

        void btnSetup2Back_Click(object sender, EventArgs e)
        {
            setup1.Visible = true;
            setup2.Visible = false;
        }

        void btnSetup1Next_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = Users.GetUser(UserName);
            if (userInfo == null)
            {
                SiteContext.RenderMessage(ExceptionType.UserNotFound);
                return;
            }

            lbQuestion.Text = Enum<QuestionType>.GetEnumStr(userInfo.PasswordQuestion);

            setup1.Visible = false;
            setup2.Visible = true;

            Page.Session.Add(sessionKey, userInfo);
        }

        private string UserName
        {
            get
            {
                if (txtUserName != null)
                    return txtUserName.Text.Trim();
                else
                    return string.Empty;
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "User/UserGetPassword.ascx"; }
        }
    }
}